// 系统级别存储，例如：页面头部tag，左菜单是否折叠
import { defineStore } from 'pinia'
// import screenfull from 'screenfull'
import { useRouter } from 'vue-router'
export const useSystemStore = defineStore(
  'system',
  () => {
    const router = useRouter()
    const routerTagList = ref([])
    const isCollapse = ref(false)
    const isFullScreen = ref(false)

    // 新增删除tag
    const updateRouterTag = (route, type = 'add') => {
      let idx = -1
      idx = routerTagList.value.findIndex(
        (item) => item.fullPath === route.fullPath
      )
      if (type === 'add' && idx < 0) {
        routerTagList.value.push(route)
      } else if (type === 'del' && idx >= 0) {
        routerTagList.value.splice(idx, 1)
      }
    }
    // 清除tag
    const clearRouterTag = (type, item) => {
      if (type === 'noSelf') {
        routerTagList.value = routerTagList.value.filter(
          (tag) => tag.fullPath === item.fullPath
        )
      } else if (type === 'left') {
        let idx = routerTagList.value.findIndex(
          (tag) => tag.fullPath === item.fullPath
        )
        routerTagList.value = routerTagList.value.slice(idx)
      } else if (type === 'right') {
        let idx = routerTagList.value.findIndex(
          (tag) => tag.fullPath === item.fullPath
        )
        routerTagList.value = routerTagList.value.slice(0, idx)
      }
      router.push(item.fullPath)
    }
    // 切换菜单展开状态
    const toggleCollapse = () => {
      isCollapse.value = !isCollapse.value
    }

    return {
      routerTagList,
      isCollapse,
      isFullScreen,
      updateRouterTag,
      clearRouterTag,
      toggleCollapse,
    }
  },
  {
    persist: {
      enabled: true, //Store中数据持久化生效
    },
  }
)
